package LeetCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class LC_078_Subsets {
    public static void main(String[] args) {

    }

    static class Solution {
        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> res = new LinkedList<>();
            List<Integer> tmpList = new LinkedList<>();
            Arrays.sort(nums);
            dfs(res, nums, tmpList, 0);
            return res;
        }

        private void dfs(List<List<Integer>> res, int[] nums, List<Integer> item, int index) {
            if (index == nums.length) {
                res.add(new ArrayList<>(item));
                return;
            }
            dfs(res, nums, item, index + 1);
            item.add(nums[index]);
            dfs(res, nums, item, index + 1);
            item.remove(item.size() - 1);
        }
    }

    static class Solution_2 {
        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> result = new LinkedList<>();
            Arrays.sort(nums);
            process(0, nums, result, new LinkedList<>());
            return result;
        }

        private void process(int index, int[] nums, List<List<Integer>> result, List<Integer> item) {
            result.add(new LinkedList<>(item));
            for (int i = index; i < nums.length; i++) {
                item.add(nums[i]);
                process(i + 1, nums, result, item);
                item.remove(item.size() - 1);
            }
        }
    }
}
